package com.example.smarttrafficgitee;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.smarttrafficgitee.fragments.ContentFragment;
import com.example.smarttrafficgitee.fragments.MenuFragment;
import com.example.smarttrafficgitee.fragments.P01ETCAcountFragment;
import com.example.smarttrafficgitee.fragments.P02TrafficSignalsFragment;
import com.example.smarttrafficgitee.fragments.P03RechargeLogFragment;
import com.example.smarttrafficgitee.fragments.P04ViolationFragment;
import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ResideMenu resideMenu;
    Button btn_LeftMenu_Open;
    List<Fragment> fragmentList;
    //每添加新fragment先修改部分
    ResideMenuItem p01ETCAcount_item, p02TrafficSignals_item, p04Violation_item, p03RechargeLog_item;
    ResideMenuItem[] resideMenuItems = new ResideMenuItem[]{p01ETCAcount_item, p02TrafficSignals_item, p04Violation_item, p03RechargeLog_item};
    String[] title = {"p01ETCAcount", "p02TrafficSignals", "p03RechargeLog", "p04Violation"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addFragment();
        setUpMenu();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.menuFrameLayout, new MenuFragment())
                .commit();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.contentFrameLayout, new ContentFragment())
                .commit();

    }
//无需修改部分，切换fragment
    private void setUpMenu() {
        btn_LeftMenu_Open = findViewById(R.id.title_bar_left_menu);
        //
        resideMenu = new ResideMenu(this);
        resideMenu.attachToActivity(this);
        resideMenu.setBackground(R.drawable.menu_background);
        //关闭右侧划动
        resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);
        //
        for (int i = 0; i < resideMenuItems.length; i++) {
            resideMenuItems[i] = new ResideMenuItem(this, R.drawable.icon_residemenu, title[i]);//实例化
            resideMenuItems[i].setOnClickListener(this);//添加到监听
            resideMenu.addMenuItem(resideMenuItems[i], ResideMenu.DIRECTION_LEFT); //添加到左侧菜单栏,先添加在前 ResideMenu.DIRECTION_LEFT
            resideMenuItems[i].setTag(i);
        }
        //左侧按钮监听器，打开菜单
        btn_LeftMenu_Open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });
    }

    @Override
    public void onClick(View v) {
        ResideMenuItem resideMenuItem = (ResideMenuItem) v;
        changefragment(fragmentList.get((Integer) resideMenuItem.getTag()));
        resideMenu.closeMenu();
    }
    //每添加新fragment先修改部分
    private void addFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new P01ETCAcountFragment());
        fragmentList.add(new P02TrafficSignalsFragment());
        fragmentList.add(new P03RechargeLogFragment());
        fragmentList.add(new P04ViolationFragment());//TEST
    }

    //改fragment视图
    private void changefragment(Fragment fragment) {
        resideMenu.clearIgnoredViewList();
        getSupportFragmentManager().beginTransaction().replace(R.id.contentFrameLayout, fragment, null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }
}
